package com.bdcom.autotest.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Set;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import com.bdcom.autotest.MainFrame;
import com.bdcom.autotest.interaction.serial.ui.SerialPortFrame;
import com.bdcom.autotest.interaction.step.FlowBean;
import com.bdcom.autotest.ui.snp.*;
import com.bdcom.autotest.util.CUtil;
import com.bdcom.autotest.util.Prop;
import com.bdcom.autotest.util.UIUtil;

public class TopPanel extends JPanel{
	
	private MainFrame mf;
	private JPanel panel;
	
	private JComboBox<String> selectBox ;
	private JLabel dateLable ;
	private JLabel userLabel ;
	private JLabel baudLabel ;

	
	private JLabel chassisLabel;
	private JLabel tipLabel1;
	private PanelForVersion panelForVersion = new PanelForVersion();
	public TopPanel( MainFrame mf ) {
		
		this.mf = mf;
		
		this.setPreferredSize(new Dimension( 0 , 50) );
	
		this.setLayout(new BorderLayout() );
		
		panel = new JPanel(  );
		this.add(panel);
		JSeparator createHSep = UIUtil.createHSep();
		this.add(createHSep , BorderLayout.SOUTH);
		initComponent();
		
		
		UIUtil.setCompBack(panel);
		UIUtil.setCompBack(this);
	}

	private void initComponent() {
		
//		Set<String> keySet = Prop.flows.keySet();
		String[] array = new String[Prop.menus.size() ];
		array = Prop.menus.toArray( array );
		
		selectBox = new JComboBox<>( array );
		selectBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				if("主控测试".equals(selectBox.getSelectedItem().toString())){
//					new Thread() {
//						@Override
//						public void run() {
//							int result = JOptionPane.showConfirmDialog( mf , ("是否为S8503机框") ,"确认信息" , JOptionPane.YES_NO_OPTION);
//							if( JOptionPane.YES_OPTION == result ){
//								changeFlow("主控测试2");
//							}else{
//								changeFlow("主控测试");
//							}
//						}
//					}.start();
//				}else{
					changeFlow(  );
//				}

			}
		});
		
		panel.setLayout(new BorderLayout());
		JPanel p1 = UIUtil.createPanel();
		p1.setLayout(new FlowLayout(FlowLayout.LEFT));
		JPanel p2 = UIUtil.createPanel();
		p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
//
//		p1.add(Box.createHorizontalStrut( 10 ));
//		p1.add(selectBox);
//		p1.setPreferredSize(new Dimension( 900,0  ) );
//
//		p1.add(Box.createHorizontalStrut( 20 ));
//		chassisLabel = new JLabel("机框：未识别");
//		chassisLabel.setFont(UIUtil.font2 );
//		String labelStr = "";
//		if(Prop.testType == 1){
//			labelStr = "基测测试中...";
//		}else{
//			labelStr = "复测测试中...";
//		}
//		tipLabel1 = new JLabel(labelStr);
//		tipLabel1.setFont(UIUtil.font2 );
		
//		JButton btn = new JButton("配置打流端口");
//		btn.addActionListener( new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				JDialog frame = new JDialog();
//				frame.setTitle("配置打流端口");
//				frame.setLocation(600,200);
////					窗体大小
//				frame.setSize(300,320);
////					显示窗体
//				PanelForSetPort panelForSetPort = new PanelForSetPort();
//				JPanel pp = (JPanel) panelForSetPort;
//				frame.add(pp);
//				frame.setAlwaysOnTop(true);
//				frame.setVisible(true);
//				panelForSetPort.loadPortConfig();
//			}
//		});
//		p1.add(chassisLabel);
//		p1.add(Box.createHorizontalStrut( 20 ));
//		p1.add(tipLabel1);
//		p1.add(btn);

		JButton btn = new JButton("配置本机网卡");
		btn.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				JDialog frame = new JDialog();
				frame.addWindowListener(new WindowAdapter() {
					public void windowClosing(WindowEvent e) {
						frame.dispose();
					}
				});
				frame.setTitle("配置本机网卡");
				frame.setLocation(600,200);
//					窗体大小
				frame.setSize(700,150);
//					显示窗体
				PanelForSelectNetCard panelForSelectNetCard = new PanelForSelectNetCard();
				JPanel pp = (JPanel) panelForSelectNetCard;
				frame.add(pp);
				frame.setAlwaysOnTop(true);
				frame.setVisible(true);
			}
		});
		p1.add(Box.createHorizontalStrut( 20 ));
		p1.add(btn);

		JButton serverBtn = new JButton("查看服务器参数");
		serverBtn.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JDialog frame = new JDialog();
				frame.addWindowListener(new WindowAdapter() {
					public void windowClosing(WindowEvent e) {
						frame.dispose();
					}
				});
				frame.setTitle("查看服务器参数");
				frame.setLocation(600,200);
//					窗体大小
				frame.setSize(530,400);
//					显示窗体
				PanelForSetConsoleServer panelForSetConsoleServer = new PanelForSetConsoleServer();
				JPanel pp = (JPanel) panelForSetConsoleServer;
				frame.add(pp);
//				frame.setAlwaysOnTop(true);
				frame.setVisible(true);
			}
		});
		p1.add(Box.createHorizontalStrut( 20 ));
		p1.add(serverBtn);

//		JButton btn2 = new JButton("查看波特率");
//		btn2.addActionListener( new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				JDialog frame = new JDialog();
//				frame.setTitle("修改波特率");
//				frame.setLocation(600,200);
////					窗体大小
//				frame.setSize(300,320);
////					显示窗体
//				PanelForBaudRate panelForBaudRate = new PanelForBaudRate();
//				JPanel pp = (JPanel) panelForBaudRate;
//				frame.add(pp);
//				frame.setAlwaysOnTop(true);
//				frame.setVisible(true);
//			}
//		});
//		p1.add(Box.createHorizontalStrut( 20 ));
//		p1.add(btn2);

		JButton btn3 = new JButton("按工单号导出改型结果");
		btn3.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JDialog frame = new JDialog();
				frame.addWindowListener(new WindowAdapter() {
					public void windowClosing(WindowEvent e) {
						frame.dispose();
					}
				});
				frame.setTitle("按工单号导出改型结果");
				frame.setLocation(600,200);
//					窗体大小
				frame.setSize(300,150);
//					显示窗体
				PanelForExportByWorkOrder panelForExportByWorkOrder = new PanelForExportByWorkOrder();
				JPanel pp = (JPanel) panelForExportByWorkOrder;
				frame.add(pp);
				frame.setAlwaysOnTop(true);
				frame.setVisible(true);
			}
		});
		p1.add(Box.createHorizontalStrut( 20 ));
		p1.add(btn3);

		JLabel label = new JLabel("服务器波特率:");
		p1.add(label);
		baudLabel = new JLabel("");
		p1.add(baudLabel);
//		JButton btn2 = new JButton("配置串口");
//		btn2.addActionListener( new PanelForBaudRate() {
//			public void actionPerformed(ActionEvent e) {
//				mf.serialPort.setVisible(true);
//			}
//		});
//		p1.add(btn2);
		
//		JButton btn3 = new JButton("版本信息");
//		btn3.addActionListener( new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
////					显示窗体
//				panelForVersion.setVisible(true);
//			}
//		});
//		p2.add(btn3);
		
		dateLable = new JLabel( CUtil.getDateStr() );// + " " +CUtil.getTimeStr()
		userLabel = new JLabel( Prop.username  );
		p2.add(dateLable);
		p2.add(Box.createHorizontalStrut( 20 ));
		p2.add(userLabel);
		p2.add(Box.createHorizontalStrut( 20 ));
		
		panel.add(p1 , BorderLayout.WEST);
		panel.add(p2);
		
		
		
	}


	public void changeFlow(   ) {
		String flowName = selectBox.getSelectedItem().toString();
		FlowBean flowBean = Prop.flows.get(flowName);
		mf.setflow( flowBean );
	}
	
	public void changeFlow(String flowName) {
		FlowBean flowBean = Prop.flows.get(flowName);
		mf.setflow( flowBean );
	}
	
	public void clickCheckBox() {
		int selectedIndex = selectBox.getSelectedIndex();
		selectBox.setSelectedIndex(selectedIndex);
	}

	public void setcheckbox(boolean b) {
		selectBox.setEnabled(b);
	}

	public void setChassisLabel(String chassis) {
		chassisLabel.setText(chassis);
	}

	public void setBaudLabel(String baud){
		baudLabel.setText(baud);
	}

}
